import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path : '/login',
      name : 'login',
      meta : {
        title : '登录',
      },
      //@是vite配置的，代表src
      component : () => import('@/views/Login.vue')
    },{
      path : '/register',
      name : 'register',
      meta : {
        title : '注册',
      },
      //@是vite配置的，代表src
      component : () => import('@/views/Register.vue')
    },{
      path:'/',
      redirect:'noteClass'
    },{
      path:'/noteClass',
      component: () => import('@/views/NoteClass.vue'),
      meta:{
        title:'笔记分类'
      }
    },{
      path:'/NoteList',
      component: () => import('@/views/NoteList.vue'),
      meta:{
        title:'笔记列表'
      }
    },{
      path:'/noteDetail',
      component: () => import('@/views/NoteDetail.vue'),
      meta:{
        title:'笔记详情'
      }
    },{
      path:'/notePublish',
      component: () => import('@/views/notePublish.vue'),
      meta:{
        title:'笔记发布'
      }
    }
  ]
})

//白名单
const whitePath = ['/login','/register','/noteClass']
//全局路由守卫
//三种 全局前置beforeEach 全局解析beforeResovle 全局后置afterEach
router.beforeEach((to,from,next) => {
  //页面tab
  document.title = to.meta.title;
  //next 放行 koa洋葱模型类似
  // console.log(to); 
  if(!whitePath.includes(to.path)){ //需要登录
    //判断是否登录过
    if(!localStorage.getItem('userInfo')){
      router.push('/login');
    }else{
      //放行
      next()
    }
  }
  next()  
})

export default router
